﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.Store
{
    public partial class 废料入库单 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = 0;
            bool 发货 = false;
            string 日期;
            int 入库单位ID;         
            string 备注;
            string sql = "select '清空选择'as 废料名称,'0' as ID union SELECT [废料名称],(cast([ID] as varchar)+'*'+[单位]+'*'+isnull([规格],'')+'*'+cast(dbo.[废料季度单价](废料.ID,@年度,@月份) as varchar)+'*'+cast(isnull(dbo.[废料分配数量](废料.ID,@分厂ID,@年度,@月份),0) as varchar)) as ID from [废料]  order by [废料名称] desc";
            if (!this.IsPostBack)
            {
                this.Button_Save.Attributes.Add("onclick", "calc();");
                this.ShouHuoDanWei.DataBind();
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out id);
                }
                if (id > 0)
                {
                    this.RowList.Items.Clear();
                    this.ShouHuoDanWei.DataBind();
                    this.BillID.Text = id.ToString();
                    this.SqlDataSource1.SelectParameters["ID"].DefaultValue = this.BillID.Text;
                    this.DanHao.Text = Request.QueryString["DID"];

                    this.DanHao.Enabled = false;
                    this.SqlDataSource1.SelectCommand = @"
                    select  cast(t1.废料ID as varchar)
                    +'|=*'+isnull(t2.废料名称,'')
                    +'|=*'+isnull(t2.规格,'')
                    +'|=*'+isnull(t2.单位,'')
                    +'|=*'+cast(t1.数量 as varchar)
                    +'|=*'+cast(t1.单价 as varchar)           
                    +'|=*'+isnull(t1.备注,'') 
                    +'|=*'+cast(t1.日期 as varchar)
                    +'|=*'+cast(t3.名称 as varchar)
                     as val                   
                    ,'' as txt
                     from 废料到发记录 t1
                    left outer join 废料 t2 on t1.废料ID=t2.ID 
                    left outer join 生产单位 t3 on t3.ID=t1.入库单位ID             
                     where t1.ID=@ID";
                    DateTime d = DateTime.Today;
                    this.Date1.Text = d.ToLongDateString();

                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;
                    Cmd.CommandText = "select 发货,日期,入库单位ID,备注 from 废料到发记录 where ID=" + id;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {
                        发货 = Reader.GetBoolean(0);
                        日期 = Reader.GetDateTime(1).ToString("yyyy年MM月dd日");
                        入库单位ID = Reader.GetInt32(2);
              
                        if (Reader.IsDBNull(3))
                        {
                            备注 = "";
                        }
                        else
                        {
                            备注 = Reader.GetString(3);
                        }
                        this.ShouHuoDanWei.SelectedValue = 入库单位ID.ToString();                
                        this.Date1.Text = 日期;
                        this.BeiZhu.Text = 备注;

                    }
                    Conn.Close();
                    Cmd.Dispose();
                    Conn.Dispose();
                   
                }
                else
                {
                    DateTime d = DateTime.Today;
                    this.Date1.Text = d.ToLongDateString();
                    sql = sql.Replace("@年度", Request.QueryString["year"]);
                    sql = sql.Replace("@月份", Request.QueryString["month"]);
                    sql = sql.Replace("@分厂ID", this.ShouHuoDanWei.SelectedItem.Value);

                    this.SqlDataSource3.SelectCommand = sql;
                }
            }
          
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            string[] cell_id = this.Cell_FID.Value.Split(new string[] { "|" }, StringSplitOptions.None);

            string[] cell_pm = this.Cell_PM.Value.Split(new string[] { "|=*" }, StringSplitOptions.None);
            string[] cell_xh = this.Cell_XH.Value.Split(new string[] { "|=*" }, StringSplitOptions.None);
            string[] cell_dw = this.Cell_DW.Value.Split(new string[] { "|=*" }, StringSplitOptions.None);

            string[] cell_sl = this.Cell_SL.Value.Split(new string[] { "|" }, StringSplitOptions.None);
            string[] cell_dj = this.Cell_DJ.Value.Split(new string[] { "|" }, StringSplitOptions.None);

            DateTime d;
            if (!DateTime.TryParse(this.Date1.Text, out d))
            {
                this.Message.Text = "填报日期 录入有误!";
                return;
            }
            //if (this.DanHao.Text == "")
            //{
            //    this.Message.Text = "单号不能为空 请输入单号!";
            //    return;
            //}

            if (Request.QueryString["id"] != null)
            {
                this.RowList.Items.Clear();
                for (int i = 1; i < cell_id.Length; i++)
                {
                    ListItem li = new ListItem();
                    li.Text = cell_id[i] + "|=*" + cell_pm[i] + "|=*" + cell_xh[i] + "|=*" + cell_dw[i]
                        + "|=*" + cell_sl[i] + "|=*" + cell_dj[i];
                    li.Value = "";
                    this.RowList.Items.Add(li);
                }

            }
            try
            {
                for (int i = 1; i < cell_id.Length; i++)
                {
                    if (this.BillID.Text != "0")
                    {
                        this.SqlDataSource1.UpdateParameters["ID"].DefaultValue = this.BillID.Text;

                        this.SqlDataSource1.UpdateParameters["废料ID"].DefaultValue = cell_id[i];
                        this.SqlDataSource1.UpdateParameters["入库单位ID"].DefaultValue = this.ShouHuoDanWei.Text;
                        this.SqlDataSource1.UpdateParameters["日期"].DefaultValue = d.ToShortDateString();
                        this.SqlDataSource1.UpdateParameters["数量"].DefaultValue = cell_sl[i];
                        this.SqlDataSource1.UpdateParameters["单价"].DefaultValue = cell_dj[i];


                        if (this.SqlDataSource1.Update() > 0)
                        {
                            this.Message.Text = "保存成功!";
                            this.Button_Save.Enabled = false;
                        }
                        else
                        {
                            this.Message.Text = "保存失败!";
                            this.Button_Save.Enabled = true;
                        }
                    }
                    else
                    {
                        string op = (string)this.Session["OperatorName"];
                        if (op == null)
                        {
                            op = "";
                        }
                        this.SqlDataSource1.InsertParameters["保管员"].DefaultValue = op;
                        this.SqlDataSource1.InsertParameters["单据号"].DefaultValue = "0000000".Substring(this.BillID.Text.ToCharArray().Length) + this.BillID.Text;
                        this.SqlDataSource1.InsertParameters["废料ID"].DefaultValue = cell_id[i];
                        this.SqlDataSource1.InsertParameters["入库单位ID"].DefaultValue = this.ShouHuoDanWei.Text;
                        this.SqlDataSource1.InsertParameters["日期"].DefaultValue = d.ToShortDateString();
                        this.SqlDataSource1.InsertParameters["数量"].DefaultValue = cell_sl[i];
                        this.SqlDataSource1.InsertParameters["单价"].DefaultValue = cell_dj[i];
                        this.SqlDataSource1.InsertParameters["备注"].DefaultValue = this.BeiZhu.Text;
                        if (this.SqlDataSource1.Insert() > 0)
                        {
                            this.Message.Text = "保存成功!";
                            this.Button_Save.Enabled = false;
                        }
                        else
                        {
                            this.Message.Text = "保存失败!";
                            this.Button_Save.Enabled = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.Message.Text = "保存失败，请仔细检查核对!";
                return;
                throw (ex);
            }

        }
       
    }
}
